<?php
class User_Model_Users extends Zend_Db_Table{
	protected $_name = 'users';
	protected $_primary = 'user_id';
	
	//payment co su dung
	public function selectUserInfoByUserID($id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUserInfoByUserID/id/".$id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);

		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
			
		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}				
		return $data[0];		
	}
	public  function selectUserInfo($u)
	{// co dung

		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUserInfo/u/".$u);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];

		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}
		return $data[0];		
	}
	
	/*public  function checkLogin($u,$p)
	{
		$select=$this->select();
		$select->from("users");
		$select->setIntegrityCheck(false);
		$select->where('user_name = ?', $u);
		$select->where('password = ?', $p);
		$arr=$this->fetchAll($select);
		return $arr;
	}
	*/
	public function getUser($username)
	{// co dung

		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/getUser/username/".$username);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
		
		return $data;		
	}
	
	public function getEmail($email)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/getEmail/email/".$email);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
		
		return $data;			
	}
	
	public function insertUser($data)
	{// co dung
		$data['route'] = "insertUser";
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restPost("/users", $data);

		return $return->getBody();
	}
	public function selectUser($id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUser/id/".$id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}			
		return $data;		
	}
	
	public function updateAccount($user_id, $data)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		if(isset($data['address']) && $data['address']!=null)
		{
			$data['address'] = str_replace("/", "~", $data['address']);
		}
		if(isset($data['avatar']) && $data['avatar']!=null)
		{
			$data['avatar'] = str_replace("/", "~", $data['avatar']);
		}
		$temp = serialize($data);
		$return = $client->restPut("/users/route/updateAccount/user_id/".urlencode($user_id)."/data/".urlencode($temp));
		return $return->getBody();		
	}
	
	//Upload avatar
	public function uploadAvatar($directory, $imageName)
	{
		$upload = new Zend_File_Transfer_Adapter_Http();     
		$upload->setDestination($directory,$imageName);
		$upload->addValidator('ImageSize', false, array('minwidth' => 10, 
                                                        'maxwidth' => 800,
                                                        'minheight' => 10,
                                                        'maxheight' => 800));    
    	if(!$upload->isValid()){
    		return -1;
		}
        $newImageName = "avatar_" . md5(time()).strstr($imageName, '.');
        $upload->addFilter('Rename', array('target'=> $directory . $newImageName,'overwrite' => true));
        $upload->receive($imageName);
       	return $newImageName;
   	}

   /*	public function selectUserAvartaByUserID($user_id)
	{
		$select = $this->select();
		$select->from('users');
		$select->setIntegrityCheck(false);
		$select->where('user_id = ?', $user_id);
		$arr = $this->fetchRow($select);
		return $arr->avatar;
	}*/
	
   /*public function count_users ()
   {
   	$select=$this->select();
			$select->from ('users')	
				->setIntegrityCheck(false);
			$arr= $this->fetchAll($select);
			$kq=count($arr);
			return  $kq;
   }*/
   
}
?>